using System;
using AtDevice;

namespace GtkControls
{
    
    
    public partial class TextMessageNewWindow : Gtk.Window
    {
        
        public TextMessageNewWindow(string number, string message) : 
                base(Gtk.WindowType.Toplevel)
        {
            this.Build();
            messageTextBox.Buffer.Changed += delegate { UpdateCounter(); };
            
            messageTextBox.Buffer.Text = message;
            numberEntry.Text = number;
        }
                
        private void UpdateCounter()
        {
            statusLabel.Text = String.Format("{0} characters used", messageTextBox.Buffer.Text.Length);
        }

        protected virtual void OnSendButtonClicked (object sender, System.EventArgs e)
        {
            DeviceHandler.Get().GetSMSInteface().SendSMS(numberEntry.Text, messageTextBox.Buffer.Text);
            statusLabel.Text = "Message sent!";
            CloseForm(400);
        }

        protected virtual void OnSaveButtonClicked (object sender, System.EventArgs e)
        {
            DeviceHandler.Get().GetSMSInteface().SaveSMS(numberEntry.Text, messageTextBox.Buffer.Text);
            statusLabel.Text = "Message saved!";
        }

        protected virtual void OnCloseButtonClicked (object sender, System.EventArgs e)
        {
            this.Destroy();
        }
                
        private void CloseForm(int interval)
        {
            System.Timers.Timer timer = new System.Timers.Timer(interval);
            timer.Elapsed += delegate { this.Destroy(); };
            timer.Start();
        }
    }
}
